/**
 * @fileoverview 基于jQuery的模拟下拉列表插件
 * @author Fengwp 360buy UED
 * @version 1.0
 * @example
 * $('.fake-select-input').fakeSelect();
 */
/*global jQuery:true */
(function($) {
	/**
	 * 模拟下拉主函数
	 * @param {Array}options 用户传进来的参数
	 */
	$.fn.fakeSelect = function(options, param) {
		options = options || {};
		return this.each( function() {
			var state = $.data(this, 'select');
			var opts;
			if (state) {
				opts = $.extend(state.options, options);
				state.options = opts;
			} else {
				opts = $.extend({}, $.fn.fakeSelect.defaults, {
					width:$(this).width()
				}, options);
				$.data(this, 'select', {
					options: opts
				});
			}
			var FS=new fakeSel(this,opts);
			FS.init();
		});
	};
	/**
	 * 模拟下拉默认参数
	 */
	$.fn.fakeSelect.defaults = {
		speed:0,
		padding:16,
		listWidth:0,
		listHeight:0,
		onSelect:function(){}//this 指向的是input控件
	};

	function fakeSel(obj,opts){
		this.s = $(obj);
		this.ul = $(obj).next();
		this.opts = opts;
	}
	fakeSel.prototype.init=function(){
		var self = this;
		self.s.bind('click',function(e){
			e.stopPropagation();//1.全部隐藏 2.键盘操作时先判断可见性
			self.show();
		})
	}
	fakeSel.prototype.show=function(){
		var self=this;
		if(!this.s.attr('disabled')){
			var position = this.s .position();
			var width = this.opts.listWidth?this.opts.listWidth:(this.s.width()+this.opts.padding);
			var height = this.opts.listHeight?this.opts.listHeight:this.ul.height();
			this.ul.css({
				"left":position.left,
				"top": position.top+this.s.height(),
				"width":width,
				"height":height
			}).addClass('_flag');
			$('._flag').slideUp(this.opts.speed)
			this.ul.slideDown(this.opts.speed);
			$(document).bind('keyup',function(e){
				var key=e.keyCode;
				if(self.ul.is(":visible")){
					var i = -1,lis=self.ul.find('li'),l=lis.length;
					if(self.ul.find('li.selected').length) i = lis.index(self.ul.find('li.selected'));
					switch(key){
						case 38://上
							i=i>0?i-1:i
							$(lis[i]).addClass('selected').siblings().removeClass('selected');
							self.s.val($(lis[i]).find('span.val').text()).removeClass('noticeInfor');
							break;
						case 40: //下
							i=i<(l-1)?i+1:(l-1);
							$(lis[i]).addClass('selected').siblings().removeClass('selected');
							self.s.val($(lis[i]).find('span.val').text()).removeClass('noticeInfor');
							break;
						case 13: //回车
							self.s.val(self.ul.find('li.selected span.val').text()).removeClass('noticeInfor');
							self.hide();
							break;
						default: //默认
							break;
					}
				}
			})
			.bind('click',function(){
				self.hide();
			});
			self.liEvent();
		}
	}
	fakeSel.prototype.hide=function(){
		this.ul.hide();
		$(document).unbind('click').unbind('keyup');
		this.ul.find('li').unbind('click').unbind('mouseover').removeClass('over');
		this.opts.onSelect.call(this.s);
	}
	fakeSel.prototype.liEvent=function(){
		var self = this;
		self.ul.find('li')
		.bind('click',function(){
			$(this).addClass('selected').siblings().removeClass('selected');
			self.s.val(self.ul.find('li.selected span.val').text()).removeClass('noticeInfor');
		})
		.bind('mouseover',function(){
			$(this).addClass('over').siblings().removeClass('over');
		});
		self.ul.find('li:odd').addClass('odd');
	}
})(jQuery);